package com.zjs.express.dao.impl;

import com.zjs.express.bean.Admin;
import com.zjs.express.dao.AdminDao;
import com.zjs.express.utils.DBUtils;
import com.zjs.express.utils.DateUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

public class AdminDaoImpl extends DBUtils implements AdminDao {
    /**
     * 更新登录时间
     *
     * @param id
     * @param loginTime
     * @return
     */
    @Override
    public boolean updateLoginTimeById(String id, Date loginTime, String ip) {
        String sql = "update admin set login_time = ? ,login_ip=? where id = ?";
        boolean update = false;
        try {
            update = update(sql, new Object[]{DateUtils.formatDate(loginTime), ip, id});
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return update;
    }

    @Override
    public Admin listByUsername(String username) {
        String sql = "select * from admin where username = ?";
        ResultSet resultSet = query(sql, new Object[]{username});
        Admin admin = null;
        try {
            admin = new Admin();
            while (resultSet.next()) {
                admin.setId(resultSet.getString("id"));
                admin.setUsername(resultSet.getString("username"));
                admin.setPassword(resultSet.getString("password"));
                admin.setRealName(resultSet.getString("real_name"));
                admin.setPhone(resultSet.getString("phone"));
                admin.setLoginIp(resultSet.getString("login_ip"));
                admin.setLoginTime(resultSet.getDate("login_time"));
                admin.setCreateTime(resultSet.getDate("create_time"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return admin;
    }
}
